//
//  NRWFliterAutoLabelView.m
//  NRWMall
//
//  Created by 张记 on 2018/6/7.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWFliterAutoLabelView.h"
#import "NRWSubScriptionLabelCell.h"
#import "NRWFilterScrollModel.h"

@interface NRWFliterAutoLabelView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionview;

@end

@implementation NRWFliterAutoLabelView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self Initcollectionview];
    }
    return self;
}
-(void)setModelArray:(NSArray<NRWFilterScrollModel*>*)modelArray{
    _modelArray = modelArray;
    
    [self.collectionview reloadData];
    
}

-(void)Initcollectionview{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //横排滚动
    _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH(), self.frame.size.height) collectionViewLayout:layout];
    _collectionview.backgroundColor = backColor();
    [_collectionview registerNib:[UINib nibWithNibName:@"NRWSubScriptionLabelCell" bundle:nil] forCellWithReuseIdentifier:@"NRWSubScriptionLabelCell"];
    _collectionview.showsHorizontalScrollIndicator = NO;
    

    if (@available(iOS 11.0, *)) {
        _collectionview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    }
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    [self addSubview:_collectionview];
}


#pragma mark - CollectionDelegateAndSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.modelArray.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NRWSubScriptionLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NRWSubScriptionLabelCell" forIndexPath:indexPath];
    cell.index = indexPath;
    cell.LabelTitle = self.modelArray[indexPath.row].title;
    cell.isShowClose = self.isShowClose;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat Width = [NRWWordSizeTool countTextWidth:self.modelArray[indexPath.row].title font:13.0] + 20;
    return CGSizeMake( Width, 50);
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section

{
    return UIEdgeInsetsMake(0, 10, 0, 10);//分别为上、左、下、右
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}

@end
